<template>
    <NuxtLayout name="blank">
        <div :class="isDark ? 'layout-bg-dark' : 'layout-bg-light'">
            <div v-if="error" class="flex items-center justify-center h-full">
                <el-empty :description="error" />
            </div>

            <chats
                v-if="!pageLoading"
                :identity="identity"
                :permits="permits"
                :secrets="secrets"
                :robot="robotDetail"
            />

            <login ref="loginRef" @confirm="handleLogin" />
        </div>
    </NuxtLayout>
</template>

<script setup lang="ts">
import { cacheEnum } from '~/enums/cache'
import useUserStore from '~/stores/user'
import useConfStore from '~/stores/conf'
import chatDialogApi from '~/api/agent/chat/dialog'
import Chats from './_components/chats.vue'
import Login from './_components/login.vue'

const route = useRoute()
const userStore = useUserStore()
const confStore = useConfStore()
const loginRef = shallowRef<InstanceType<typeof Login>>()

// 暗黑模式
const isDark = computed(() => confStore.isDarkColor)

// 访问信息
const identity = ref<string>(userStore.getVisitUid())
const permits = shallowRef(String(route.params.key))
const secrets = ref<string>('')
const error = ref<string>('')

// 页面加载中
const pageLoading = ref<boolean>(true)

// 智能体信息
const robotDetail = ref({} as ChatPublishVerifyResponse)

/**
 * 处理页面登录
 */
const handleLogin = async (data: any) => {
    const cookie = useCookie(cacheEnum.PERMITS_KEY)
    const jsonDt: any = Object.assign(
        cookie.value || {},
        {[permits.value]: data.password}
    )

    secrets.value = data.password
    cookie.value = jsonDt
    loginRef.value?.close()
    await handleInitial()
}

/**
 * 处理初始请求
 */
const handleInitial = async () => {
    pageLoading.value = true
    const cookie: any = useCookie(cacheEnum.PERMITS_KEY)

    if (cookie.value === undefined) {
        loginRef.value?.open()
        return
    }

    secrets.value = cookie.value[permits.value] || ''
    const result = await chatDialogApi.verify({
        Permits: permits.value,
        Secrets: secrets.value,
        Identity: identity
    })

    switch(parseInt(result.code)) {
        case 1101: // ApiKey过期
        case 1100: // ApiKey无效
        case 1300: // 身份鉴权失败
        case 1:    // 其它错误信息
            error.value = result.msg
            feedback.msgError(result.msg)
            break
        case 1200: // 访问密码错误
            loginRef.value?.open()
            feedback.msgError(result.msg)
            break
        case 0:
            robotDetail.value = result.data
            pageLoading.value = false
            break
    }
}

onMounted(async () => {
    await handleInitial()
})
</script>

<style scoped lang="scss">
.layout-bg-light {
    height: 100vh;
    background: linear-gradient(
        to right,
        #f5fbff, #f3fbff,
        #faf8fe, #fef7fe
    );
}

.layout-bg-dark {
    height: 100vh;
    background: linear-gradient(
        to right,
        #1e2124, #23262a,
        #24272b, #222529
    );
}
</style>
